import util from '../utils/util';
import request from '../utils/request';
import api from '../config/api';

/**
 * Lottery detail
 */
function detail(lotteryId) {
  return new Promise((resolve, reject) => {
    request(api.Path.LotteryDetail, {
      lotteryId
    }, 'GET')
    .then(res => resolve(res))
    .catch(err => reject(err));
  });
}

/**
 * 发起抽奖
 */
function launch(data) {
  return new Promise((resolve, reject) => {
    request(api.Path.LaunchLottery, data, 'POST')
    .then((res) => resolve(res))
    .catch(err => reject(err));
  });
}

/**
 * 购买 vip
 */
function buyVip() {
  return new Promise((resolve, reject) => {
    request(api.Path.BuyVip, {}, 'POST')
      .then((res) => resolve(res))
      .catch(err => reject(err));
  });
}
/**
 * 编辑抽奖 
 */
function edit(data){
  return new Promise((resolve, reject) => {
    request(api.Path.EditLottery, data, 'PUT')
    .then((res) => resolve(res))
    .catch(err => reject(err));
  });
}

/**
 * 参加抽奖
 */
function join(lotteryId) {
  return new Promise((resolve, reject) => {
    request(api.Path.JoinLottery, {
      lotteryId
    }, 'POST')
    .then(res => resolve(res))
    .catch(err => reject(err));
  })
}

/**
 * 发起手动抽奖，只用抽奖发起者可以调用该方法
 */
function draw(lotteryId) {
  return new Promise((resolve, reject) => {
    request(api.Path.DrawLottery, {
      lotteryId
    }, 'POST')
    .then(res => resolve(res))
    .catch(err => reject(err));
  })
}
function upload(imgPath){
  const token = wx.getStorageSync('token');
  return new Promise((resolve, reject) => {
    wx.uploadFile({
      url: 'https://choujiang.jiamutiancheng.com/upload', 
      filePath: imgPath,
      name: 'file',
      header: {
        'Authorization': token ? `Bearer ${token}` : null,// 登录令牌
        'content-type':"multipart/form-data"
      },
      success: function (res) {
        resolve(res);
      },
      fail: function (err){
        reject(err)
      }
    })
  })
}
function payStatus(orderId){
  return new Promise((resolve, reject) => {
    request(api.Path.PayStatus, {
      orderId
    }, 'GET')
      .then(res => resolve(res))
      .catch(err => reject(err));
  })  
}
export default {
  launch,
  join,
  detail,
  draw,
  edit,
  upload,
  buyVip,
  payStatus
}
